window.addEventListener("load", init, false);

function init()
{
    document.getElementById("registered_true").addEventListener("click", switchRegTrue, false);
    document.getElementById("registered_false").addEventListener("click", switchRegFalse, false);
    document.getElementById("login").addEventListener("keyup", changePseudo, false);
}

function switchRegTrue()
{
    document.getElementById("persoDiv").style.display = "block";
    document.getElementById("loginDiv").style.display = "block";
    document.getElementById("passwordDiv").style.display = "block";
    document.getElementById("send").style.display = "inline";
}

function switchRegFalse()
{
    document.getElementById("persoDiv").style.display = "block";
    document.getElementById("loginDiv").style.display = "block";
    document.getElementById("passwordDiv").style.display = "none";
    document.getElementById("send").style.display = "inline";
}

function sendData()
{
    if(document.getElementById("registered_true").checked)
    {
        alert("1");
    }
    if(document.getElementById("registered_false").checked)
    {
        alert("2");
    }
    
}
function getXMLHttpRequest() {
	var xhr = null;
	
	if (window.XMLHttpRequest || window.ActiveXObject) {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			xhr = new XMLHttpRequest(); 
		}
	} else {
		alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
		return null;
	}
	
	return xhr;
}


function changePseudo()
{
    var url = document.getElementById("login").value;
    if(url != ""){
        var xhr = getXMLHttpRequest();

        xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                       changeIndicatorPseudo(xhr.responseText);
                }
        };
        xhr.open("GET", "http://localhost:57272/index.php/lobby/checkAvailability/"+url, true);
        xhr.send(null);
    }
}

function changeIndicatorPseudo(response)
{
    if(response == 1)
    {
        document.getElementById("indicator_Login").className = "green";
        document.getElementById("indicator_Login").innerHTML = "Ce pseudonyme est disponible";
    }
    else
    {
        document.getElementById("indicator_Login").className = "red";
        document.getElementById("indicator_Login").innerHTML = "Ce pseudonyme n'est pas disponible !";
    }
}